import sys

from PyQt6.QtCore import QSize,Qt
from PyQt6.QtGui import QAction
from PyQt6.QtWidgets import *

if __name__ == '__main__':
    class MainWindow(QMainWindow):

        def __init__(self):

            super().__init__()
            self.setWindowTitle("My App")

            self.button = QPushButton("Press me")

            # 按钮状态
            self.button.setCheckable(True)
            self.button.clicked.connect(self.the_button_was_clicked)
            self.button.clicked.connect(self.the_button_was_toggled)
            # button.released.connect(self.the_button_was_released)

            # 改变窗口标题事件
            self.windowTitleChanged.connect(self.the_window_title_changed)

            self.setFixedSize(QSize(400, 300))


            self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
            # 鼠标右键信号菜单
            self.customContextMenuRequested.connect(self.on_context_menu)

            # label QVBoxLayout
            self.label = QLabel()
            self.label2 = QLabel("click in this window")

            self.input = QLineEdit()
            # input change event to setText
            self.input.textChanged.connect(self.label.setText)

            layout = QVBoxLayout()
            layout.addWidget(self.input)
            layout.addWidget(self.label)
            layout.addWidget(self.button)
            layout.addWidget(self.label2)

            container = QWidget()
            container.setLayout(layout)


            self.setCentralWidget(container)


        def on_context_menu(self,pos):
            context = QMenu(self)
            context.addAction(QAction("test=== 1", self))
            context.addAction(QAction("test 2", self))
            context.addAction(QAction("test 3", self))
            context.exec(self.mapToGlobal(pos))

        def the_window_title_changed(self, window_title):
            print("Window title changed: %s" % window_title)

            if window_title == 'Something went wrong':
                self.button.setDisabled(True)

        def the_button_was_clicked(self):
            print("click")
            print("Setting title:  %s","=========")
            self.setWindowTitle("========")

        def the_button_was_toggled(self,checked):
            print("Checked?", checked)


        def the_button_was_released(self):
            print("Released" + str(self.button.isChecked()))

        # 窗口事件
        def mouseMoveEvent(self, e):
            self.label.setText("mouseMoveEvent")

        def mousePressEvent(self, e):
            self.label.setText("mousePressEvent")

        def mouseReleaseEvent(self, e):
            self.label.setText("mouseReleaseEvent")

        def mouseDoubleClickEvent(self, e):
            self.label.setText("mouseDoubleClickEvent")

        # 鼠标右键菜单
        def contextMenuEvent(self, e):
            context = QMenu(self)
            context.addAction(QAction("test 1", self))
            context.addAction(QAction("test 2", self))
            context.addAction(QAction("test 3", self))
            context.exec(e.globalPos())

    app = QApplication(sys.argv)

    window = MainWindow()
    # 窗口置顶
    window.setWindowFlags(window.windowFlags() | Qt.WindowType.WindowStaysOnTopHint)
    window.show()
    # 设置活跃窗口
    window.raise_()
    window.activateWindow()
    app.exec()
